﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Zero.Code;
using Zero.Model;

namespace Zero.DAL
{
    public class Organization
    {
        public string[] ShowOrganizationInfo()
        {
            zerodbEntities db=new zerodbEntities();

            string[] str = new string[3];
            str[0] = (from o in db.organization select o).ToJson();
            str[1] = (from g in db.grades select g).ToList().ToJson();
            str[2] = (from c in db.classes select c).ToList().ToJson();
            return str;
        }
        /// <summary>
        /// 通过机构ID获取机构信息集合
        /// </summary>
        /// <param name="OrganizedID">机构ID</param>
        /// <returns></returns>
        public List<organization> GetOrganizationByID(int OrganizedID)
        {
            List<organization> list = new List<organization>();
            using (var db=new zerodbEntities())
            {
                organization organization = null;
                if (OrganizedID == 0)
                {
                    var organized = from o in db.organization
                                    select o;
                    foreach (var item in organized)
                    {
                        organization = new organization();
                        organization = item;
                        list.Add(organization);
                    }
                }
                else
                {
                    var organized = from o in db.organization
                                    where o.O_Id == "O_"+OrganizedID.ToString()
                                    select o;
                    list.Add(organized.FirstOrDefault());
                }
                var itaem =list;
                return list;
            }
        }
        /// <summary>
        /// 通过角色ID获取机构集合
        /// </summary>
        /// <param name="RoleID">角色ID</param>
        /// <returns></returns>
        public List<organization> GetOrganizationsByRoleID(int RoleID)
        {
            using (var db = new zerodbEntities())
            {
                var role = from r in db.role
                           where r.R_Id == RoleID
                           select r;
                var item = role.FirstOrDefault();
                return GetOrganizationByID(item.R_OrganizedId.ToInt());
            }
        }
    }
}
